package model;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class TextSplitter{
	
	private LinkedList<Word> wordList;

	public TextSplitter(final String text){
		this.splitText(text);
	}
	
	public List<Word> splitText(final String text){
		
		int wortStartIndex = 0;
		int wortEndIndex = 0;
		final int orderNumber = 0;
		final LinkedList<Character> seperators = new LinkedList<Character>();
		seperators.add('\r');
		seperators.add('\n');
		seperators.add('\t');
		
		while(wortEndIndex>=0){
			if(wortStartIndex == wortEndIndex)
				wortEndIndex = wortStartIndex + 1;
			else{
			wortEndIndex = getIndex(text, seperators);
			wordList.add(Word.create(text.substring(wortStartIndex, wortEndIndex), new Occurence(wortStartIndex, wortEndIndex, orderNumber),"german"));
			wortStartIndex = wortEndIndex;
			}
		}
		
		return wordList;
		
	}
	
	public int getIndex(final String text, final LinkedList<Character> seperator){
		final LinkedList<Integer> index = new LinkedList<Integer>();
		
		final Iterator<Character> seperatorIterator = seperator.iterator();
		while(seperatorIterator.hasNext()){
			final Character current = seperatorIterator.next();
			if(text.indexOf(current)>-1)
				index.add(text.indexOf(current));
		}
		Collections.sort(index);
		return index.getFirst();
		
	}
	
}
